<?php
class EventHandler {

	public function ListEventDB() {

		//tar kontakt med databasen
		$DBConnection = new DBConnection();
		$DBConnection -> Connect();

		//ställer en sql-fråga.
		$sql = "SELECT * FROM `event` LIMIT 0, 30 ";

		$stmt = $DBConnection -> prepare($sql);
		if ($stmt === FALSE) {
			//echoBr("prepare of '$sql' failed " .  $DBConnection -> error);
			return false;
		}
		$stmt -> execute();
		// ger resultet.
		$result = $stmt -> get_result();
		$ret = array();

		while ($row = $result -> fetch_array(MYSQLI_NUM)) {
			$ret[] = $row;
		}
		$stmt -> close();
		return $ret;
	}

	public function DoAddToEvent($username,$name,$lastname) {


		//tar kontakt med databasen
		$DBConnection = new DBConnection();
		$DBConnection -> Connect();
		//ställer en sql-fråga.

		$sql = "INSERT INTO `event`(`username`,`m_username`, `m_lastname`) VALUES (?,?,?)";
		//$sql = "INSERT INTO `event`(`pk_Event`, `username`, `m_username`, `m_lastname`) VALUES (\" \",\"k\",\"k\",\"k\")";
		$stmt = $DBConnection -> prepare($sql);
		$stmt -> bind_param("sss",$username ,$name,$lastname);

		if ($stmt === FALSE) {
			//echoBr("prepare of '$sql' failed " .  $DBConnection -> error);
			return false;
		}
		$stmt -> execute();
		// ger resultet.
		$result = $stmt -> get_result();
		/*
		 while ($row = $result -> fetch_array(MYSQLI_NUM)) {
		 $stmt -> close();
		 return true;
		 }
		 *
		 */
		$stmt -> close();
		return false;
	}

	public static function DeleteFromEvent($name) {
		//tar kontakt med databasen
		$DBConnection = new DBConnection();
		$DBConnection -> Connect();

		//ställer en sql-fråga.
		$sql = "DELETE FROM `event` WHERE `username` = ?";

		$stmt = $DBConnection -> prepare($sql);
		$stmt -> bind_param("s", $name);

		if ($stmt === FALSE) {
			//echoBr("prepare of '$sql' failed " .  $DBConnection -> error);
			return false;
		}
		$stmt -> execute();
		// ger resultet.
		$result = $stmt -> get_result();
		/*
		 while ($row = $result -> fetch_array(MYSQLI_NUM)) {
		 $stmt -> close();
		 return true;
		 }
		 *
		 */
		$stmt -> close();
		return false;
	}

}
